/**
 * Created by Yu Jin Xian on 2016/12/12.
 */
function pattern() {
    var pattern1 = /at/g;                           // 匹配字符串中所有 "at" 的实例
    var pattern2 = /[bc]at/i;                       // 匹配第一个 "bat" 或 "cat", 不区分大小写
    var pattern3 = /.at/gi;                         // 匹配所有以 "at" 结尾的 3 个字符的组合, 不区分大小写

    function escape() {
        var pattern1 = /[bc]at/i;                   // 匹配第一个 "bat" 或 "cat", 不区分大小写
        var pattern2 = /\[bc\]at/i;                 // 匹配第一个 "[bc]at", 不区分大小写
        var pattern3 = /.at/gi;                     // 匹配所有以 "at" 结尾的 3 个字符的组合, 不区分大小写
        var pattern4 = /\.at/gi;                    // 匹配所有以 ".at", 不区分大小写
    }

    function constructor() {
        var pattern1 = /[bc]at/i;                   // 匹配第一个 "bat" 或 "cat", 不区分大小写
        var pattern2 = new RegExp('[bc]at', 'i');   // 与 parttern1 相同, 只不过是使用构造函数创建的
    }

    var re = null, i;
    for (i = 0; i < 10; i++) {
        re = /cat/g;
        re.test('catastrophe');
    }
    for (i = 0; i < 10; i++) {
        re = new RegExp('cat', 'g');
        re.test('catastrophe');
    }
}

function instanceProperty() {
    var pattern1 = /\[bc\]at/i;
    alert(pattern1.global);                         // false
    alert(pattern1.ignoreCase);                     // true
    alert(pattern1.multiline);                      // false
    alert(pattern1.lastIndex);                      // 0
    alert(pattern1.source);                         // "\[bc\]at"

    var pattern2 = new RegExp('\\[bc\\]at', 'i');
    alert(pattern1.global);                         // false
    alert(pattern1.ignoreCase);                     // true
    alert(pattern1.multiline);                      // false
    alert(pattern1.lastIndex);                      // 0
    alert(pattern1.source);                         // "\[bc\]at"
}

function instanceMethods() {
    function exec() {
        var text = 'mon and dad and bady';
        var pattern = /mon( and dad( and bady)?)?/gi;
        var matches = pattern.exec(text);
        alert(matches.index);                       // 0
        alert(matches.input);                       // "mon and dad and baby"
        alert(matches[0]);                          // "mon and dad and bady"
        alert(matches[1]);                          // " and dad and bady"
        alert(matches[2]);                          // " and bady"
    }

    function execGlobal() {
        var text = 'cat, bat, sat, fat';
        var pattern = /.at/;

        var matches = pattern.exec(text);
        alert(matches.index);                       // 0
        alert(matches[0]);                          // cat
        alert(matches.lastIndex);                   // 0

        matches = pattern.exec(text);
        alert(matches.index);                       // 0
        alert(matches[0]);                          // cat
        alert(matches.lastIndex);                   // 0

        function global() {
            var pattern = /.at/g;

            var matches = pattern.exec(text);
            alert(matches.index);                       // 0
            alert(matches[0]);                          // cat
            alert(matches.lastIndex);                   // 3

            matches = pattern.exec(text);
            alert(matches.index);                       // 5
            alert(matches[0]);                          // bat
            alert(matches.lastIndex);                   // 8
        }
    }

    function test() {
        var text = '000-00-0000';
        var pattern = /\d{3}-\d{2}-\d{4}/;
        if (pattern.test(text)) {
            alert('The pattern was matched');
        }
    }

    function toString() {
        var pattern = new RexReg('\\[bc\\]at', 'gi');
        alert(pattern.toString());                  // /\[bc]at/gi
        alert(pattern.toLocaleString());            // /\[bc]at/gi
    }

    function constructorProperties() {
        function fullName() {
            var text = 'this has been a short summer';
            var pattern = /(.)hort/g;
            if (pattern.test(text)) {
                alert(RegExp.input);                    // this has been a short summer
                alert(RegExp.leftContext);              // this has been a
                alert(RegExp.rightContext);             // summer
                alert(RegExp.lastMatch);                // shourt
                alert(RegExp.lastParen);                // s
                alert(RegExp.multiple);                 // false
            }
        }


        function shortName() {
            var text = 'this has been a short summer';
            var pattern = /(.)hort/g;
            if (pattern.test(text)) {
                alert(RegExp['$_']);                // this has been a short summer
                alert(RegExp['$`']);                // this has been a
                alert(RegExp["$'"]);                // summer
                alert(RegExp['$&']);                // shourt
                alert(RegExp['$+']);                // s
                alert(RegExp['$*']);                // false
            }
        }

        function paren() {
            var text = 'this has benn a shourt summer';
            var pattern = /(.)or(.)/g;
            if (pattern.test(text)) {
                alert(RegExp.$1);                   // sh
                alert(RegExp.$2);                   // t
            }
        }
    }
}